
package com.xtsoft.platform.base.system.service.impl;
/*
*    上海信拓信息技术有限公司 Koala企业综合信息管理系统
*    Copyright (C) 2010-2015 ShangHai XinTuo Software Inc.
*/



import com.xtsoft.core.service.impl.GenericServiceImpl;
import com.xtsoft.core.utils.StringUtils;
import com.xtsoft.core.utils.tree.CommonTreeNode;
import com.xtsoft.core.utils.tree.TreeBuilder;
import com.xtsoft.platform.base.system.domain.SysOrganizeType;
import com.xtsoft.platform.base.system.repository.SysOrganizeTypeRepository;
import com.xtsoft.platform.base.system.service.SysOrganizeTypeService;
import com.xtsoft.platform.globe.Constants.Constants;
import com.xtsoft.platform.globe.enums.YesNo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;


@Service("sysOrganizetypeService")
public class SysOrganizeTypeServiceImpl extends GenericServiceImpl<SysOrganizeType,String,SysOrganizeTypeRepository> implements SysOrganizeTypeService {

	@Autowired
	public SysOrganizeTypeRepository sysOrganizetypeRepository;

	@Override
	public List<CommonTreeNode> getTree() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException {
		// TODO Auto-generated method stub
		List<SysOrganizeType> sysOrganizeTypes = sysOrganizetypeRepository.findByState(YesNo.Yes.getIntValue());
		//StringBuffer buffs=new StringBuffer("[{\"id\":\""+com.xtsoft.common.Constants.RootNode+"\",\"text\":\"企业类型\",\"iconCls\":\"x-fa fa-th-list\",\"expanded\": true,\"leaf\":false,\"children\":");
		//String TreeResource[] = {"NodeId","ParentNodeId","NodeName","OrganizeTypeId","","",""};
		//com.xtsoft.web.rest.util.TreeBuilder<SysOrganizeType> treeBuilder = new com.xtsoft.web.rest.util.TreeBuilder<SysOrganizeType>();
		TreeBuilder<SysOrganizeType> treeBuilder = new TreeBuilder();
		SysOrganizeType sysOrganizeType = new SysOrganizeType();
		sysOrganizeType.setNodeId(Constants.RootNode);
		sysOrganizeType.setNodeName("企业类型");
		sysOrganizeType.setOrganizeTypeId(Constants.RootNode);
		sysOrganizeTypes.add(sysOrganizeType);
		List<CommonTreeNode> menuList = treeBuilder.build(sysOrganizeTypes, "organizeTypeId", "nodeId", "parentNodeId",
				"nodeName",null,"serialNo",null);
		return menuList;
	}

	@Override
	public void addSysOrganizeType(SysOrganizeType sysOrganizetype) {
		// TODO Auto-generated method stub
		String nodeId = "";
//		if(sysOrganizetype.getParentNodeId().equals(com.xtsoft.common.Constants.RootNode)) {
//			nodeId = NodeUtils.createNodeIdWithParent(sysOrganizetypeRepository.findMaxNodeId(sysOrganizetype.getParentNodeId()),sysOrganizetype.getParentNodeId(),com.xtsoft.common.Constants.CodeLengthTwo,true,true);
//		}else {
//			nodeId = NodeUtils.createNodeIdWithParent(sysOrganizetypeRepository.findMaxNodeId(sysOrganizetype.getParentNodeId()),sysOrganizetype.getParentNodeId(),com.xtsoft.common.Constants.CodeLengthTwo,true,false);
//		}
		sysOrganizetype.setNodeId(nodeId);
		if(StringUtils.isEmpty(sysOrganizetype.getSerialNo())) {
			//找到父节点下子节点的最大排序号
			Integer serialNo = sysOrganizetypeRepository.findSerialNoByParentNodeIdMax(sysOrganizetype.getParentNodeId());
			if(StringUtils.isEmpty(serialNo)){
				sysOrganizetype.setSerialNo(1);
			}else {
				sysOrganizetype.setSerialNo(serialNo+1);
			}
			
		}
	}
	

}